﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BluePortal.Medias.Core;
using BluePortal.Framework;

namespace BluePortal.Medias.Core
{
    public class BCChannelUser : AbstractBusinessComponent
    {
        #region Internal Methods
        public IQueryable<ChannelUser> QueryFindAll()
        {
            var query = from t in Context.ChannelUsers
                        select t;
            return query;
        }

        public IQueryable<ChannelUser> QueryFindByChannel(Channel channel)
        {
            AssertNotNull(channel, "channel");
            return QueryFindByChannel(channel.CHID);
        }

        public IQueryable<ChannelUser> QueryFindByChannel(Guid gid)
        {
            var query = from t in Context.ChannelUsers
                        where t.CHID == gid
                        select t;
            return query;
        }

        public IQueryable<ChannelUser> QueryFindByStatus(UserStatus status)
        {
            var query = from t in Context.ChannelUsers
                        where t.ApprovedID == (int)status
                        select t;
            return query;
        }
        #endregion

        #region IMediaChannelUserService Members

        public void AddUser(Guid gid, string loginName)
        {
            var channel = Context.Channels.Single(c => c.CHID == gid);
            Context.ChannelUsers.InsertOnSubmit(new ChannelUser() { CHID = gid, LoginName = loginName });
            Context.SubmitChanges();
        }

        public void AddUser(Channel channel, ChannelUser user)
        {
            AssertNotNull(channel, "channel");
            AssertNotNull(user, "user");
            AddUser(channel.CHID, user.LoginName);
        }

        public void DeleteUser(Guid gid, string loginName)
        {
            var user = Context.ChannelUsers.SingleOrDefault(c => c.CHID == gid && c.LoginName == loginName);
            if (user != null)
            {
                Context.ChannelUsers.DeleteOnSubmit(user);
                Context.SubmitChanges();
            }
        }

        public void UpdateStatus(ChannelUser user, UserStatus status)
        {
            AssertNotNull(user,"user");
            var currentUser = Context.ChannelUsers.SingleOrDefault(u => u.CHID == user.CHID && u.LoginName == user.LoginName);
            if (currentUser != null)
            {
                currentUser.ApprovedID = (int)status;
                Context.SubmitChanges();
            }
        }

        public FindResult<ChannelUser> FindAll(FindParameters f)
        {
            return ToFindResult<ChannelUser>(QueryFindAll(), f);
        }

        public FindResult<ChannelUser> FindByChannel(Channel channel, FindParameters f)
        {
            return ToFindResult<ChannelUser>(QueryFindByChannel(channel), f);
        }

        public FindResult<ChannelUser> FindByChannel(Guid gid, FindParameters f)
        {
            return ToFindResult<ChannelUser>(QueryFindByChannel(gid), f);
        }

        #endregion
    }
}
